using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class GameState : MonoBehaviour {

    public int shots;
    public int penalties;
    public bool win;
    public bool gameOver;

    public GameObject scoreCount;
    public GameObject shotsCount;
    public GameObject penaltiesCount;
    public GameObject gameOverLabel;
    public GameObject penaltyDescriptionLabel;
    public GameObject redScoreX;

    Text shotsText;
    Text penaltiesText;
    Text scoreText;
    Text gameOverText;
    Text penaltyDescriptionText;
    Text redScoreXText;

	// Use this for initialization
	void Start ()
    {
        shots = 0;
        penalties = 0;

        scoreText = scoreCount.GetComponent<Text>();
        shotsText = shotsCount.GetComponent<Text>();
        penaltiesText = penaltiesCount.GetComponent<Text>();
        gameOverText = gameOverLabel.GetComponent<Text>();
        redScoreXText = redScoreX.GetComponent<Text>();

        gameOverText.text = "";
        penaltyDescriptionText = penaltyDescriptionLabel.GetComponent<Text>();
        penaltyDescriptionText.text = "";
        redScoreXText.enabled = false;
	}

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            SceneManager.LoadScene("menu", LoadSceneMode.Single);
            return;
        }
	}

    public void Potted(BallColour.Colour colour)
    {
        if (colour == BallColour.Colour.Yellow)
        {
            int redCount = 0;
            var balls = FindObjectsOfType<BallColour>();
            foreach (BallColour ball in balls)
            {
                if (ball.colour == BallColour.Colour.Red)
                {
                    redCount++;
                }
            }

            SetPenaltyText("Potted a yellow. " + redCount.ToString() + " red balls in play. " + redCount.ToString() + " shot penalty.");
            AddPenalty(redCount);
            //StartCoroutine("NewYellowBall");
        }
        if (colour == BallColour.Colour.White)
        {
            SetPenaltyText("Potted the cue ball. 2 shot penalty.");
            AddPenalty(2);
        }
        else if (colour == BallColour.Colour.Black)
        {
            bool stillRedBalls = false;
            var balls = FindObjectsOfType<BallColour>();
            foreach (BallColour ball in balls)
            {
                if (ball.colour == BallColour.Colour.Red)
                {
                    stillRedBalls = true;
                    break;
                }
            }
            win = (false == stillRedBalls);
            if (false == win) SetPenaltyText("Potted the black without potting all the reds first. You lose!");
            GameOver();
        }
        Redraw();
    }

    IEnumerator NewYellowBall()
    {
        yield return new WaitForSeconds(0.75f);
        float theta = Random.Range(0.0f, 360.0f);
        float r = Random.Range(1.5f, 3.5f);

        var rotation = Quaternion.Euler(0, 0, theta);
        var v = Vector3.right * r;
        v = rotation * v;

        Instantiate(Resources.Load("YellowBall"), v, Quaternion.identity);
    }

    void GameOver()
    {
        gameOver = true;
        redScoreXText.enabled = !win;

        gameOverText.text = win ? "YOU\nWIN!" : "GAME\nOVER";
    }

    void Redraw()
    {
       shotsText.text = shots.ToString();
       penaltiesText.text = penalties.ToString();
       scoreText.text = (shots + penalties).ToString();
    }

    public void ShotShotted()
    {
        shots++;
        Redraw();
    }

    public void AddPenalty(int p)
    {
        penalties += p;
        Redraw();
    }

    public void SetPenaltyText(string text)
    {
        penaltyDescriptionText.text = text;
    }
}
